class ConceptController < ApplicationController
  def lookup
    @concept = Concept.find(params[:concept_id])    
  end

  def create_map
    unless params[:concept_id_1].blank? || params[:concept_id_2].blank?
      @concept = Concept.find(params[:concept_id_1])
      @to_concept = Concept.find(params[:concept_id_2])
      @comments = params[:map_comments]
      map = Map.find :first, :conditions => {:from_fragment_id => @concept.fragment.id, :from_local_key => @concept.local_key, :to_fragment_id => @to_concept.fragment.id, :to_local_key => @to_concept.local_key }
      if map.nil? # only create the mapping if it doesn't exist yet
        Map.create(:from_fragment_id => @concept.fragment.id, :from_local_key => @concept.local_key, :to_fragment_id => @to_concept.fragment.id, :to_local_key => @to_concept.local_key, :user_id => @user.id, :mapping_type_id => params[:mapping_type_id], :comments => @comments)
      end
    end
  end

  def new_map
    unless params[:concept_id_1].blank? || params[:concept_id_2].blank?
      @concept = Concept.find(params[:concept_id_1])
      @to_concept = Concept.find(params[:concept_id_2])
      @fresh_user = User.find session[:user].id #need to make sure that we get a fresh instance of the user from db in case their default mapping_type changed
    end
  end  

  def destroy_map
    @concept = Concept.find(params[:concept_id_1])
    @to_concept = Concept.find(params[:concept_id_2])
    Map.delete_all :from_fragment_id => @concept.fragment.id, :from_local_key => @concept.local_key, :to_fragment_id => @to_concept.fragment.id, :to_local_key => @to_concept.local_key, :mapping_type_id => params[:mapping_type_id].to_i
    render :partial => "mappings"
  end
end
